<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\FeishuMini\ProgrammingEnum;
use App\Models\BaseModel;
use App\Models\Common\MapNationalBrandShopModel;
use App\Models\Common\MapNationalCommunityModel;
use App\Models\Common\MapNationalIndustrialParkModel;
use App\Models\Common\MapNationalStreetModel;
use App\Models\Common\MapNationalUniversitiesModel;
use App\Models\Common\MapTownshipPlanAreaModel;
use App\Models\Common\ResourcesModel;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\SiteLocation\GuanyuanDataService;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
use Kuafu\File\FileUtil;

class WangguiMapImageDataSupplement extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:map-image-data-supplement';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网规点位地图图片数据补充';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //        $this->error('谨慎');
        //        dd(404);
        ini_set('memory_limit', '1024M');
        $num = 1;


        // ⚠️ 不要用公司的key

        MapNationalCommunityModel::query()
            ->chunk(100, function ($communities) use (&$num) {
                foreach ($communities as $community) {
                    $lng = $community->lng;
                    $lat = $community->lat;
                    if (mb_strlen($community->name) > 15) {
                        $label = mb_substr($community->name, 0, 12) . '...';
                    } else {
                        $label = $community->name;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $community->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $community->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');



        //数据修复
        $brandIds = [];
//
//        MapNationalBrandShopModel::query()
//            ->with(['resourceWithMapRel'])
//            ->chunk(100, function ($shops) use (&$num){
//                $this->info('ing....');
//                foreach ($shops as $shop) {
//                    if ($shop->resourceWithMapRel != null) {
//                        continue;
//                    }
//                    $lng     = $shop->lng;
//                    $lat     = $shop->lat;
//                    if (mb_strlen($shop->name) > 15) {
//                        $label = mb_substr($shop->name, 0, 12) . '...';
//                    } else {
//                        $label = $shop->name;
//                    }
//                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
//                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
//                    // 获取在线文件内容
//                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
//                    // 重命名文件
//                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
//                    $this->info($num);
//                    $num++;
//                    // 文件存储的路径
//                    $savePath = sys_get_temp_dir() . '/' . $fileName;
//
//                    // 存文件
//                    file_put_contents($savePath, $tempData);
//                    $fileArr            = FileUtil::uploadFile($savePath);
//                    $resource           = ResourcesModel::query()->firstOrNew([
//                        'name' => $shop->wgNumber
//                    ]);
//                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
//                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
//                    $resource->name     = $shop->wgNumber;
//                    $resource->createdByUid     = 999;
//                    $resource->url      = $fileArr['path'];
//                    $resource->isPublic = 1;
//                    $resource->save();
//                    // 删除文件
//                    @unlink($savePath);
//                }
//            });
//
//        dd(1212);













        MapNationalBrandShopModel::query()
            ->when(1, function ($query) {
                $child = ResourcesModel::query()
                    ->where('name', 'like', '%PP77%')
                    ->where('createdByUid', 999)
                    ->where('url', '26/e9/1b08af7f08bbf3769e8cc1476603310c879f3961448fcc034b1ed809cf7f.png')
                    ->select('name');
                $query->whereRaw("wgNumber in (" . BaseModel::toSqlWithParam($child) . ')');
            })
            ->chunk(1000, function ($brand) use (&$num) {
                foreach ($brand as $shop) {
                    $lng     = $shop->lng;
                    $lat     = $shop->lat;
                    if (mb_strlen($shop->name) > 15) {
                        $label = mb_substr($shop->name, 0, 12) . '...';
                    } else {
                        $label = $shop->name;
                    }
                    $characters = array(' ', '+', '-', '|', '&', ',');
                    $label = str_replace($characters, '', $label);
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $shop->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $shop->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });



dd(1231312);









        //街道刷取  绍兴 嘉兴  金华  常州  宜昌  襄阳   东莞  西安  南通  青岛  太原  田间  成都

        MapNationalStreetModel::query()
            ->where('id', '>=', 4318)
            ->chunk(1000, function ($streets) use (&$num) {
                foreach ($streets as $street) {
                    $lng = $street->lng;
                    $lat = $street->lat;
                    if (mb_strlen($street->name) > 15) {
                        $label = mb_substr($street->name, 0, 12) . '...';
                    } else {
                        $label = $street->name;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $street->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $street->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');


        MapNationalBrandShopModel::query()
            ->where('id', '>', 142768)
            ->chunk(1000, function ($brand) use (&$num) {
                foreach ($brand as $shop) {
                    sleep(0.5);
                    $lng     = $shop->lng;
                    $lat     = $shop->lat;
                    $label   = $shop->name;
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $shop->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $shop->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');

        try {
            GuanyuanDataService::setTimeOut(30);
            $kfShop  = GuanyuanDataService::newShopList();
        } catch (\Exception $exception) {
            $kfShop = [];
        }
        foreach ($kfShop as $shop) {
            $lng = $shop['门店经度'];
            $lat = $shop['门店纬度'];
            if (mb_strlen($shop['门店名称']) > 15) {
                $label = mb_substr($shop['门店名称'], 0, 12) . '...';
            } else {
                $label = $shop['门店名称'];
            }
            $name    = 'KFZC' . str_pad($shop['铺位号'], 7, '0', STR_PAD_LEFT);
            $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
            $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
            // 获取在线文件内容
            $tempData = (new Client())->request("get", $a)->getBody()->getContents();
            // 重命名文件
            $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
            $this->info($fileName);
            $this->info($num);
            $num++;
            // 文件存储的路径
            $savePath = sys_get_temp_dir() . '/' . $fileName;

            // 存文件
            file_put_contents($savePath, $tempData);
            $fileArr            = FileUtil::uploadFile($savePath);
            $resource           = ResourcesModel::query()->firstOrNew([
                'name' => $name
            ]);
            $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
            $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
            $resource->name     = $name;
            $resource->url      = $fileArr['path'];
            $resource->isPublic = 1;
            $resource->save();
            // 删除文件
            @unlink($savePath);
        }

        dd(121);


        MapNationalIndustrialParkModel::query()
            ->chunk(1000, function ($parks) use (&$num) {
                foreach ($parks as $park) {
                    $lng = $park->lon;
                    $lat = $park->lat;
                    if (mb_strlen($park->name) > 15) {
                        $label = mb_substr($park->name, 0, 12) . '...';
                    } else {
                        $label = $park->name;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $park->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $park->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');

        MapNationalUniversitiesModel::query()
            ->chunk(1000, function ($schools) use (&$num) {
                foreach ($schools as $school) {
                    $lng = $school->lon;
                    $lat = $school->lat;
                    if (mb_strlen($school->name) > 15) {
                        $label = mb_substr($school->name, 0, 12) . '...';
                    } else {
                        $label = $school->name;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $school->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $school->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');


        MapTownshipPlanAreaModel::query()
            ->where('grade', '>', 0)
            ->chunk(1000, function ($townShips) use (&$num) {
                foreach ($townShips as $townShip) {
                    $lng = $townShip->lng;
                    $lat = $townShip->lat;
                    if (mb_strlen($townShip->area) > 15) {
                        $label = mb_substr($townShip->area, 0, 12) . '...';
                    } else {
                        $label = $townShip->area;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . time() . '_' . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $townShip->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $townShip->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

        dd('OK');

        SiteLocationMallModel::query()
            ->where('grade', '>', 0)
            ->chunk(1000, function ($siteLocationArr) use (&$num) {
                foreach ($siteLocationArr as $siteLocation) {
                    $lng = $siteLocation->lng;
                    $lat = $siteLocation->lat;
                    if (mb_strlen($siteLocation->mallName) > 15) {
                        $label = mb_substr($siteLocation->mallName, 0, 12) . '...';
                    } else {
                        $label = $siteLocation->mallName;
                    }
                    $amapUrl = 'https://restapi.amap.com/v3/staticmap?location=%s,%s&markers=-1,https://file.51kuafu.com/9a/11/8f2998d7c8a1032f6eb1ebf343cd5ba8f8658c237c202c76edbb83578cc9.png,0:%s,%s&zoom=15&size=750*560&labels=%s,3,1,20,0x000000,0xFFF733:%s,%s&key=ffa97277887e1cf2b2fc0944cd86ec7d';
                    $a       = sprintf($amapUrl, $lng, $lat, $lng, $lat, $label, $lng, ($lat + 0.001));
                    // 获取在线文件内容
                    $tempData = (new Client())->request("get", $a)->getBody()->getContents();
                    // 重命名文件
                    $fileName = 'php_import_' . date('YmdHis', time()) . Str::random(5) . ".png";
                    $this->info($fileName);
                    $this->info($num);
                    $num++;
                    // 文件存储的路径
                    $savePath = sys_get_temp_dir() . '/' . $fileName;

                    // 存文件
                    file_put_contents($savePath, $tempData);
                    $fileArr            = FileUtil::uploadFile($savePath);
                    $resource           = ResourcesModel::query()->firstOrNew([
                        'name' => $siteLocation->wgNumber
                    ]);
                    $resource->module   = ResourcesModel::MODULE_TYPE_WG_AUDIT;
                    $resource->business = ResourcesModel::BUSINESS_WG_AUDIT;
                    $resource->name     = $siteLocation->wgNumber;
                    $resource->url      = $fileArr['path'];
                    $resource->isPublic = 1;
                    $resource->save();
                    // 删除文件
                    @unlink($savePath);
                }
            });

    }
}
